/**
 * 
 */
package vn.edu.hungvuongaptech.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import vn.edu.hungvuongaptech.common.Constant;
import vn.edu.hungvuongaptech.model.ProductModel;
import vn.edu.hungvuongaptech.util.DataUtil;

/**
 * @author PCHOME
 *
 */
public class ProductDAO {
	public static ArrayList<ProductModel> getAllProducts(){
		ArrayList<ProductModel> productModelList = new ArrayList<ProductModel>();
		try {
			String sql = Constant.SQL_RES.getString("timesheet.sql.getAllProducts");
			ResultSet rs = DataUtil.executeQuery(sql);
			ProductModel productModel = null;
			while (rs.next()) {
				productModel = new ProductModel();
				productModel.setProductId(rs.getInt("ProductId"));
				productModel.setProductName(rs.getString("ProductName"));
				productModel.setProductDescription(rs.getString("ProductDescription"));
				productModelList.add(productModel);
			}
			return productModelList;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ProductModel getProductByID(int productID){
		ProductModel productModel = new ProductModel();		
		try {
			PreparedStatement preparedStatement = DataUtil
					.getConnection()
					.prepareStatement(
							Constant.SQL_RES
									.getString("timesheet.sql.getProductByID"));
			preparedStatement.setLong(1, productID);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				productModel.setProductId(rs.getInt("ProductID"));
				productModel.setProductName(rs.getString("ProductName"));
				productModel.setProductDescription(rs.getString("ProductDescription"));
			}
			return productModel;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
